package com.ojoin.ojoin.eb_spider.service.catchdata;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.ojoin.ojoin.eb_spider.service.catchdata.suning.SUNINGInstore;
import com.ojoin.ojoin.eb_spider.service.catchdata.suning.SUNINGOutstore;
import com.ojoin.ojoin.eb_spider.service.catchdata.suning.SUNINGPayAccount;
import com.ojoin.ojoin.eb_spider.service.catchdata.suning.SUNINGSettlement;
import com.ojoin.ojoin.eb_spider.service.impl.SLDataCatchBaseServiceImpl;
import com.ojoin.ojoin.eb_spider.utils.WebServiceUtils;
import com.ojoin.trade.common.exception.BusinessException;
import com.ojoin.trade.common.utils.TimeUtils;
import com.ojoin.trade.supplyloan_core.web.domain.CatchDataStartMessage;
import com.ojoin.trade.supplyloan_core.web.domain.TbCatchdataStrategy;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlSupplyInventory;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlSupplyPayment;

@Service
public class SupplierSUNINGDataCatchServiceImpl extends
		SUNINGBaseDataCatchServiceImpl {
	@Autowired
	public SUNINGPayAccount pay;

	@Autowired
	public SUNINGSettlement settlement;

	@Autowired
	public SUNINGInstore instore;

	@Autowired
	public SUNINGOutstore outstore;

	private static Logger log = LoggerFactory
			.getLogger(SupplierSUNINGDataCatchServiceImpl.class);

	/**
	 * 1.付款金额：财务管理 > 付款管理 > 苏宁付款单 （抓取两个月数据）
	 * 
	 * 
	 * 2.库存金额：入库-->供应链管理》收发货管理》苏宁收货单报表（抓取三个月数据）
	 * 出库-->供应链管理》收发货管理》苏宁退货订单报表（抓取三个月数据） 入库-出库
	 * 
	 * 总计： 入库-出库-付款金额
	 */

	// 获取付款单

	@Override
	public boolean getPayAccountData() throws Exception {

		TbCatchdataStrategy ca = WebServiceUtils
				.getCatchdataStrategyByUserId(this.getAssistCatchData()
						.getStartMess().getPlatformUser().getId());

		String stime = TimeUtils.getDateBefore(ca.getPaymentInterval());
		String etime = TimeUtils.getCurDate();

		Map<String, TbSlSupplyPayment> waitpay = null;

		if (ca.getPaymentCatchAllOrPart().equals(TbCatchdataStrategy.CatchPart)) {
			waitpay = this.getAssistCatchData().getStartMess().getCompletepay();
		}

		pay.init(this.getWebDriver(), this.getAssistCatchData().getStartMess()
				.getPlatformUser(), stime, etime);

		pay.run();

		List<TbSlSupplyPayment> li = pay.result();

		pay.fiter(li, waitpay);

		this.getAssistCatchData().getCata().setPayment(li);

		return true;
	}

	@Override
	public boolean getUnpayAccountData() throws Exception {

		TbCatchdataStrategy ca = WebServiceUtils
				.getCatchdataStrategyByUserId(this.getAssistCatchData()
						.getStartMess().getPlatformUser().getId());
		String stime = TimeUtils.getDateBefore(ca.getSettlementInterval());
		String etime = TimeUtils.getCurDate();

		Map<String, TbSlSupplyPayment> waitpay = null;

		if (ca.getPaymentCatchAllOrPart().equals(TbCatchdataStrategy.CatchPart)) {
			waitpay = this.getAssistCatchData().getStartMess().getWaitpay();
		}

		settlement.init(this.getWebDriver(), this.getAssistCatchData()
				.getStartMess().getPlatformUser(), stime, etime);

		settlement.run();

		List<TbSlSupplyPayment> list = settlement.result();

		settlement.fiter(list, waitpay);

		this.getAssistCatchData().getCata().setSettlement(list);

		return true;
	}

	@Override
	public boolean getInstoreOrderData() throws Exception {
		// 送货记录
		// 对账单
		String starttime = this.getAssistCatchData().getStartMess()
				.getStoreInStartTime();

		starttime = TimeUtils.getDateBefore(2);
		
		String maxStime = TimeUtils.getDateBefore(10);

		if (TimeUtils.getDateSpaceOfDay(starttime, maxStime) > 0) {
			starttime = maxStime;
		}
		List<TbSlSupplyInventory> list = new ArrayList<TbSlSupplyInventory>();

		Map<String, TbSlSupplyInventory> map = WebServiceUtils.getSupplyInventoryByplateformId(this.getAssistCatchData().getStartMess().getPlatformUser().getId(), starttime, TimeUtils.getCurDate(),"");

		List<Map<String, String>> Timeli = getTimes(starttime, 6);

		Iterator<Map<String, String>> it = Timeli.iterator();

		String downloadDir = this.getAssistCatchData().getDownloadDir();

		while (it.hasNext()) {
			Map<String, String> timeMap = it.next();

			instore.init(this.getWebDriver(), this.getAssistCatchData()
					.getStartMess().getPlatformUser(), timeMap.get("stime"),
					timeMap.get("etime"), downloadDir);

			instore.run();

			List<TbSlSupplyInventory> li = instore.result();

			list.addAll(li);

		}

		instore.fiter(list, map);
		this.getAssistCatchData().getCata().setInstore(list);
		return true;
	}

	@Override
	public boolean getOutstoreOrderData() throws Exception {
		// 送货记录
		// 对账单
		List<TbSlSupplyInventory> list = new ArrayList<TbSlSupplyInventory>();
		String starttime = this.getAssistCatchData().getStartMess()
				.getStoreOutStartTime();

		String maxStime = TimeUtils.getDateBefore(10);

		if (TimeUtils.getDateSpaceOfDay(starttime, maxStime) > 0) {
			starttime = maxStime;
		}

		Map<String, TbSlSupplyInventory> map = WebServiceUtils.getSupplyInventoryByplateformId(this.getAssistCatchData().getStartMess().getPlatformUser().getId(), starttime, TimeUtils.getCurDate(),"");

		List<Map<String, String>> Timeli = getTimes(starttime, 6);

		Iterator<Map<String, String>> it = Timeli.iterator();
		String downloadDir = this.getAssistCatchData().getDownloadDir();

		while (it.hasNext()) {
			Map<String, String> timeMap = it.next();

			outstore.init(this.getWebDriver(), this.getAssistCatchData()
					.getStartMess().getPlatformUser(), timeMap.get("stime"),
					timeMap.get("etime"), downloadDir);

			outstore.run();

			List<TbSlSupplyInventory> li = outstore.result();

			list.addAll(li);

		}
		outstore.fiter(list, map);
		this.getAssistCatchData().getCata().setOutstore(list);
		return true;
	}
}
